// 颜色生成器
const COLORS = [
  '#FF5252', '#FF4081', '#E040FB', '#7C4DFF',
  '#536DFE', '#448AFF', '#40C4FF', '#18FFFF',
  '#64FFDA', '#69F0AE', '#B2FF59', '#EEFF41',
  '#FFFF00', '#FFD740', '#FFAB40', '#FF6E40'
];

Page({
  data: {
    items:[1,2,3,4,5],

    balls: [],
    ballCount: 5,
    centerX: 300,
    centerY: 300,
    radiusX: 250,
    radiusY: 150,
    nextId: 1,
    lastAngle: 0, // 记录上次旋转角度
    startAngle: 0, // 触摸起始角度
    isDragging: false, // 是否正在拖拽
    baseAngle: 0, // 基础旋转角度
    sensitivity: 0.5, // 旋转灵敏度(0.1-1)


  },

  onLoad() { 

    this.generateBalls(this.data.ballCount);
  },

  // 生成指定数量的小球
  generateBalls(count) {
    const newBalls = [];
    for (let i = 0; i < count; i++) {
      const angle = (i * (360 / count)) * Math.PI / 180;
      newBalls.push({
        id: this.data.nextId + i,
        x: this.data.centerX + this.data.radiusX * Math.cos(angle),
        y: this.data.centerY + this.data.radiusY * Math.sin(angle),
        angle: angle,
        scale: 1,
        zIndex: 1,
        color: COLORS[(this.data.nextId + i) % COLORS.length]
      });
    }

    this.setData({
      balls: newBalls,
      nextId: this.data.nextId + count
    });
  },

  // 改变小球数量
  changeBallCount(e) {
    const newCount = e.detail.value;
    this.setData({
      ballCount: newCount
    }, () => {
      this.generateBalls(newCount);
      this.updateBallPositions(this.data.baseAngle);
    });
  },

  // 计算缩放比例
  calculateScale(angle, count) {
    const segment = 360 / count;
    const progress = (angle * 180 / Math.PI) % segment / segment;
    return 0.6 + Math.abs(Math.sin(progress * Math.PI)) * 0.6;
  },

  // 触摸开始
  onTouchStart(e) {
    const touchX = e.touches[0].clientX;
    const touchY = e.touches[0].clientY;

    // 计算起始角度(相对于圆心)
    const angle = Math.atan2(
      touchY - this.data.centerY,
      touchX - this.data.centerX
    ) * 180 / Math.PI;

    this.setData({
      startAngle: angle,
      lastAngle: this.data.baseAngle,
      isDragging: true
    });
  },

  // 触摸移动
  onTouchMove(e) {
    if (!this.data.isDragging) return;

    const touchX = e.touches[0].clientX;
    const touchY = e.touches[0].clientY;

    // 计算当前角度(相对于圆心)
    const currentAngle = Math.atan2(
      touchY - this.data.centerY,
      touchX - this.data.centerX
    ) * 180 / Math.PI;

    // 计算角度变化(乘以灵敏度系数)
    let angleChange = (currentAngle - this.data.startAngle) * this.data.sensitivity;

    // 修正旋转方向(乘以-1)
    angleChange *= -1;

    // 更新基础角度
    const newBaseAngle = this.data.lastAngle + angleChange;
    this.setData({
      baseAngle: newBaseAngle
    });

    // 更新小球位置
    this.updateBallPositions(newBaseAngle);

    // 更新起始角度，实现连续拖拽
    this.setData({
      startAngle: currentAngle
    });
  },

  // 触摸结束
  onTouchEnd() {
    this.setData({
      isDragging: false
    });
  },

  // 更新小球位置
  updateBallPositions(baseAngle) {
    const balls = this.data.balls.map((ball, index) => {
      // 计算每个小球的角度 (基础角度 + 初始偏移)
      const angle = baseAngle * Math.PI / 180 + (index * (2 * Math.PI / this.data.ballCount));

      // 计算缩放比例
      const scale = this.calculateScale(angle, this.data.ballCount);

      return {
        ...ball,
        x: this.data.centerX + this.data.radiusX * Math.cos(angle),
        y: this.data.centerY + this.data.radiusY * Math.sin(angle),
        angle: angle,
        scale: scale,
        zIndex: Math.round(scale * 10)
      };
    });

    this.setData({
      balls
    });
  },

  onUnload() {
    // 清理工作
  },

    
});